<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>websocket客户端</title>
</head>
<body>
<script type="text/javascript">
    var webSocket;
    if (window.WebSocket) {
        webSocket = new WebSocket("ws://127.0.0.1:7397/websocket")
        webSocket.onmessage = function (event) {
            var tar = document.getElementById('respMessage');
            tar.value = tar.value + "\n" + event.data;
        }

        webSocket.onopen = function (event) {
            var tar = document.getElementById('respMessage');
            tar.value = '连接开启';
        }

        webSocket.onclose = function (event) {
            var tar = document.getElementById('respMessage');
            tar.value = '连接关闭';
        }
    } else {
        alert('浏览器不支持websocket!')
    }

    function send(message) {
        if (!window.WebSocket) {
            return;
        }
        if (webSocket.readyState == WebSocket.OPEN) {
            webSocket.send(message);
        } else {
            alert("服务端连接异常!");
        }
    }

</script>
<form onsubmit="return false;">
    <textarea name="message" style="width: 400px;height: 200px"></textarea>
    <input type="button" value="发送" onclick="send(this.form.message.value)"/>
    <h3>服务端返回内容：</h3>
    <textarea id="respMessage" style="width: 400px;height: 300px"></textarea>
    <input type="button" onclick="javascript: document.getElementById('respMessage').value=''" value="清空">
</form>
</body>
</html>